const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    team_id: '', // 小组ID
    teamDetails: '', // 小组详情
  },

  /**
   * 获取小组详情
   */
  getTeamDetails () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/team_info',
        data: {
          token,
          team_id: this.data.team_id
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'teamDetailsRes')
      let teamDetails = res.data.data ? res.data.data : []
      this.setData({
        teamDetails
      })
    }).catch(err => {
      console.log(err, 'teamDetailsErr')
    })
  },

  /**
   * 同意对方加入
   * @param {用户ID以及小组ID} e 
   */
  handleAgree (e) {
    let _this = this
    wx.showLoading({
      title: '加载中',
    })
    let user_id = e.currentTarget.dataset.user_id
    let team_id = e.currentTarget.dataset.team_id
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/examine_team',
        data: {
          token,
          user_id,
          team_id,
          team_user_stash: 1
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'agreeRes')
      _this.getTeamDetails()
      wx.hideLoading()
    }).catch(err => {
      console.log(err, 'agreeErr')
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: err.data.message,
        showCancel: false
      })
    })
  },

  /**
   * 拒绝对方加入
   * @param {用户ID以及小组ID} e 
   */
  handleRefuse (e) {
    let _this = this
    wx.showLoading({
      title: '加载中',
    })
    let user_id = e.currentTarget.dataset.user_id
    let team_id = e.currentTarget.dataset.team_id
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/examine_team',
        data: {
          token,
          user_id,
          team_id,
          team_user_stash: 2
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'refuseRes')
      _this.getTeamDetails()
      wx.hideLoading()
    }).catch(err => {
      console.log(err, 'refuseErr')
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: err.data.message,
        showCancel: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let team_id = options.team_id
    this.setData({
      team_id
    })
    this.getTeamDetails()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})